package com.zho.web.action.resource;

import com.zho.common.fileserver.FileServerBean;
import com.zho.common.fileserver.FileServerHelper;
import com.zho.common.fileserver.FilesAdapter;
import com.zho.common.fileserver.FilesManager;
import com.zho.util.Logit;
import com.zho.web.domain.model.Resource;
import com.zho.web.domain.model.UploadResourceAJAXModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UploadResourceAJAXAction extends ActionSupport implements ModelDriven<UploadResourceAJAXModel> {
    private static Logit log = Logit.getInstance(UploadResourceAJAXAction.class);
    
    private UploadResourceAJAXModel model = new UploadResourceAJAXModel();

    private FileServerHelper fileServerHelper;

    public UploadResourceAJAXModel getModel() {
        return model;
    }

	/**
	 * @param fileServerHelper the fileServerHelper to set
	 */
	public void setFileServerHelper(FileServerHelper fileServerHelper) {
		this.fileServerHelper = fileServerHelper;
	}  
	
    /**
     * 
     * @return
     */
    public String execute() {
        FilesAdapter files = null;
        String filePath = null;
        try {
            if (model.getResourceFile() != null) {
                files = FilesManager.getFileAdapter();
                // /+++ copy file to template folder

                FileServerBean fileServer = null;
                if(model.getCatType() != null) {
                	fileServer = fileServerHelper.getResourceFileServer();
                }
                fileServer = (fileServer != null ? fileServer : fileServerHelper.getResourceFileServer());
                
                String fileName = model.getResourceFileFileName();
                fileName = fileServer.toLowCaseFileExt(fileName);
                String fileType = fileServer.getFileExt(fileName);
                String fileUri = fileServer.generateFileUri(fileType);
                filePath = fileServer.getFilePath(fileUri);
                                                
                files.copyFile(model.getResourceFile().getPath(), filePath);
                                
                Resource resource = model.getResource();

                resource.setName(model.getResourceFileFileName());
                resource.setUri(fileUri);
                
                resource.setSize(files.getFileSize(filePath));

                model.setResource(resource);

            } else {
            	System.out.println("No file to upload!");
            }
            return SUCCESS;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // +++ rollback delete file
            if (files != null && filePath != null) {
                try {
                    files.delete(filePath);
                } catch (Exception ex) {
                }
            }
            return INPUT;
        } finally {
            if (files != null) {
                files.disconnect();
            }
        }
    }
}
